package ru.sviridov.sellandbuy.tasks.recipients;

import ru.sviridov.sellandbuy.LoginActivity;
import ru.sviridov.sellandbuy.MainActivity;
import ru.sviridov.sellandbuy.model.Response;
import ru.sviridov.sellandbuy.tasks.base.TaskRecipient;
import ru.sviridov.sellandbuy.tools.Constants;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.widget.Toast;
import api.istsoft.ru.dal.Auth.UserData;

public class LoginRecipient implements TaskRecipient{

	@Override
	public void onURLError(Context context, int errorCode) {
		((LoginActivity)context).showProgress(false);
	}

	@Override
	public void onJSONError(Context context, Response response) {
		((LoginActivity)context).showProgress(false);	
		Toast.makeText(context, response.getErrorMessage(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onSuccess(Context context, Response response) {
		if(context==null) return;
		LoginActivity activity = (LoginActivity)context;
		activity.storeAuth();
		Editor editor = activity.getSharedPreferences(Constants.USER_PREFS, Activity.MODE_PRIVATE).edit();
		((UserData)response.getResults()[0]).store(editor);
		editor.commit();
		activity.startActivity(new Intent(activity, MainActivity.class));
		activity.finish();
	}
	
	

}
